import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/assessments.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/models/assessment/assessmentBookType.dart';
import 'package:ichinesereader/models/book/BookItem.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/floatingActionButtonCreateAssessment.dart';
import 'package:ichinesereader/widgets/grids/assessmentBookGridWithCheckBox.dart';

class EvaluationTestsTeacherPage extends StatefulWidget {
  static const String routeName = '/teacher/evaluation/tests';

  @override
  State<EvaluationTestsTeacherPage> createState() =>
      _EvaluationTestsTeacherPageState();
}

class _EvaluationTestsTeacherPageState
    extends State<EvaluationTestsTeacherPage> {
  final AssessmentsController _controller = Get.find();
  final box = Hive.box('appconfig');
  var bookItems = <BookItem>[];

  @override
  void initState() {
    // _controller.selectedAssessmentBenchmarkTests.clear();
    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    if (isTeacher) {
      AssessmentBookType assessmentBookType = Constants.evaluationTypes
          .where((element) => element.assessmentType == 'BENCHMARK_TEST')
          .first;
      Future.delayed(Duration(milliseconds: 50), (() {
        _controller.selectedAssessmentBenchmarkTests.clear();
        _controller.setCurrentAssessmenbBookType(assessmentBookType);
        _controller
            .fetchAssessmentBenchmarkBooks(
                '0', '0', assessmentBookType.assessmentType, '')
            .then((items) {
          setState(() {
            bookItems = items;
          });
        });
      }));
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build evaluation benchmark tests TeacherPage');
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Column(
        children: [
          Expanded(
            child: _controller.obx(
              (data) => AssessmentBookGridWithCheckBox(
                books: _controller.assessmentBenchmarkTests,
                readingType: NanhaiBookReadingTypes.assessment,
              ),
              onLoading: Container(),
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButtonCreateAssessment(),
    );
  }
}
